package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.mongo.*;
import com.tanhua.domain.vo.CountsVo;
import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * 互相喜欢，喜欢，粉丝 - 统计 服务接口实现类
 */
@Service
public class UserLikeApiImpl implements UserLikeApi {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public CountsVo counts(Long userId) {
        CountsVo vo = new CountsVo();
        //互相喜欢
        Query friendQuery = new Query(Criteria.where("userId").is(userId));
        long friendCount = mongoTemplate.count(friendQuery, Friend.class);
        vo.setEachLoveCount(friendCount);//互相喜欢
        //喜欢
        Query likeQuery1 = new Query(Criteria.where("userId").is(userId));
        long likeCount1 = mongoTemplate.count(likeQuery1, UserLike.class);
        vo.setLoveCount(likeCount1);
        //粉丝
        Query likeQuery2 = new Query(Criteria.where("likeUserId").is(userId));
        long likeCount2 = mongoTemplate.count(likeQuery2, UserLike.class);
        vo.setFanCount(likeCount2);
        return vo;
    }

    /**
     * 根据userId查询好友表  互相喜欢列表
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<RecommendUser> findPageLikeEachOther(Long userId, int page, int pagesize) {
        PageResult<RecommendUser> pageResult = new PageResult<>();
        //1.查询好友表
        Query query = new Query(Criteria.where("userId").is(userId));
        Pageable pageable = PageRequest.of(page-1,pagesize);
        query.with(pageable);
        long counts = mongoTemplate.count(query, Friend.class);
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        if(friendList == null || friendList.size() == 0){
            return null;
        }
        //2.查询推荐用户表
        List<RecommendUser> recommendUserList = new ArrayList<>();
        for (Friend friend : friendList) {
            recommendUserList.add(queryScore(friend.getFriendId(),userId));
        }
        long pages = counts/pagesize + counts%pagesize > 0 ? 1:0;
        return new PageResult<RecommendUser>(counts,(long)pagesize,pages,(long)page,recommendUserList);
    }
    /**
     * 根据userId查询UserLike 喜欢列表
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<RecommendUser> findPageOneSideLike(Long userId, int page, int pagesize) {
        Query query = Query.query(
                Criteria.where("userId").is(userId))
                .limit(pagesize)
                .skip((page - 1) * pagesize);
        long total = this.mongoTemplate.count(query, UserLike.class);
        List<UserLike> list = this.mongoTemplate.find(query, UserLike.class);
        List<RecommendUser> users = new ArrayList<>();
        for (UserLike userLike : list) {
            users.add(queryScore(userLike.getLikeUserId(),userLike.getUserId()));
        }
        int pages = total / pagesize + total % pagesize > 0 ? 1 : 0;
        PageResult pageResult =
                new PageResult(total,(long)pagesize,(long)page,(long)pages,users);
        return pageResult;
    }
    /**
     * 根据userId查询UserLike 粉丝列表
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<RecommendUser> findPageFens(Long userId, int page, int pagesize) {
        Query query = Query.query(
                Criteria.where("likeUserId").is(userId))
                .limit(pagesize)
                .skip((page - 1) * pagesize);
        long total = this.mongoTemplate.count(query, UserLike.class);
        List<UserLike> list = this.mongoTemplate.find(query, UserLike.class);

        List<RecommendUser> users = new ArrayList<>();
        for (UserLike userLike : list) {
            users.add(queryScore(userLike.getUserId(),userLike.getLikeUserId()));
        }
        int pages = total / pagesize + total % pagesize > 0 ? 1 : 0;
        PageResult pageResult =
                new PageResult(total,(long)pagesize,(long)page,(long)pages,users);
        return pageResult;
    }
    /**
     * 根据userId查询visitors 谁看过我列表
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<RecommendUser> findPageMyVisitors(Long userId, int page, int pagesize) {
        Query query = Query.query(
                Criteria.where("userId").is(userId))
                .limit(pagesize)
                .skip((page - 1) * pagesize);
        List<Visitor> list = mongoTemplate.find(query, Visitor.class);
        long total = this.mongoTemplate.count(query, Visitor.class);

        List<RecommendUser> users = new ArrayList<>();
        for (Visitor visitors : list) {
            users.add(queryScore(visitors.getVisitorUserId(),userId));
        }
        int pages = total / pagesize + total % pagesize > 0 ? 1 : 0;
        PageResult pageResult =
                new PageResult(total,(long)pagesize,(long)page,(long)pages,users);
        return pageResult;
    }

    /**
     * 根据当前用户id 和 粉丝用户id  删除UserLike记录
     * @param likeUserId 粉丝id
     * @param userId 当前登录用户id
     */
    @Override
    public void delete(Long likeUserId, Long userId) {
        Query query = new Query(Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId));
        mongoTemplate.remove(query,UserLike.class);
    }


    private RecommendUser queryScore(Long userId, Long toUserId) {
        Query query = Query.query(Criteria.where("toUserId").is(toUserId).and("userId").is(userId));
        RecommendUser user = this.mongoTemplate.findOne(query, RecommendUser.class);
        if (user == null) {
            user = new RecommendUser();
            user.setUserId(userId);
            user.setToUserId(toUserId);
            user.setScore(95d);
        }
        return user;
    }
}
